package com.ghostworks.galileo.watcher;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;

public class WatcherData {
	private Connection connect = null;
	private Statement statement = null;
	private PreparedStatement preparedStatement = null;
	private ResultSet resultSet = null;	
	public WatcherData() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connect = DriverManager.getConnection("jdbc:mysql://localhost/galileo?" + "user=watcher&password=watcher123");
//			Class.forName("org.postgresql.Driver");
//			Properties props = new Properties();
//			props.setProperty("user","watcher");
//			props.setProperty("password","watcher123");
//			connect = DriverManager.getConnection("jdbc:postgresql://localhost:5432/galileo", props);
			
		} catch(Exception ex) {
			
		}
	}
	
	public ArrayList<String> getTagsByUser(String strUserId) {
		ArrayList<String> lstTags = new ArrayList<String>();
		try {
			statement = connect.createStatement();
			resultSet = statement.executeQuery("SELECT \"HTAG\" FROM \"prod\".\"USR_HTAG_LNK\"");
			while(resultSet.next()) {
				lstTags.add(resultSet.getString("HTAG"));
			}
		} catch(Exception ex) {
			System.out.println(ex.getMessage());
		}
		return lstTags;
	}
	
	public int insertTags(ArrayList<Tweet> lstTweets, String strUserID) {
		ArrayList<String> lstTags = null;

		try {
			preparedStatement = connect.prepareStatement("INSERT INTO USR_HTAG_LNK (TWEET_USR_ID, TWEET_ID, HTAG, FRIEND_ID) VALUES (?, ?, ?, ?)");
			for (Tweet tweet : lstTweets ) {
				lstTags = tweet.getStrTags();
				for (String strTag: lstTags) {
					preparedStatement.setString(1, strUserID);
					preparedStatement.setLong(2, tweet.getLongTweetID());
					preparedStatement.setString(3, strTag);
					preparedStatement.setString(4, tweet.getStrFriendID());
					preparedStatement.executeUpdate();
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return 0;
	}
	
	public Long getLastTweet(String strUserID) {
		Long lastTweet = new Long(0);
		try {
			preparedStatement = connect.prepareStatement("SELECT TWEET_ID FROM LST_TWEET WHERE USER_ID = ?");
			preparedStatement.setString(1,strUserID);
			ResultSet rsReader = preparedStatement.executeQuery();
			
			while (rsReader.next()) {
				lastTweet =  new Long(rsReader.getLong("TWEET_ID"));
			}
			System.out.println(lastTweet);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			preparedStatement = null;
		}
		
		return lastTweet;
	}
	
	public void updateLastTweet(String strUserID, Long lastTweet) {
		try {
			preparedStatement = connect.prepareStatement("UPDATE LST_TWEET SET TWEET_ID = ? WHERE USER_ID = ?");
			preparedStatement.setLong(1,lastTweet);
			preparedStatement.setString(2,strUserID);
			preparedStatement.executeUpdate();
			
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			preparedStatement = null;
		}
	}

}
